package com.example.xiaomi.Model.Service;

import com.example.xiaomi.Model.Controller.addAddressList;
import com.example.xiaomi.Model.Dao.UeserDao;
import com.example.xiaomi.Model.entity.UeserEntity;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/address/word")
public class add_modify extends HttpServlet {
    addAddressList addAddressList = new addAddressList();
    UeserDao dao = new UeserDao();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/plain;charset=utf-8");
        String str = req.getParameter("arr");
        String[] arr = str.split(",");
        PrintWriter out = resp.getWriter();
        if (arr[5].equals("添加地址")){
            String address = arr[2].replace("  ",",");
            String[] addressarr = address.split(",");
            int userid = Integer.parseInt(arr[6]);
            System.out.println(arr[6]);
            UeserEntity ueserlist =  dao.selectById(userid);
            String mail = null;
            int id = ueserlist.getUid();
            mail = ueserlist.getUpostbox();
            addAddressList.doInsert(userid,mail,addressarr[0],addressarr[1],addressarr[2],addressarr[3],arr[3],arr[4],arr[0],arr[1]);
            out.println("新增成功");
        }else{
            for (int i = 0; i < arr.length; i++) {
                System.out.println(arr[i]);
            }
            UeserEntity ueserlist =  dao.selectById(Integer.valueOf(arr[6]));
            String mail = ueserlist.getUpostbox();
            System.out.println(mail);
            int id = Integer.parseInt(arr[6]);
            String[] addAddressarr = arr[2].split("  ");
            addAddressList.doUpdate(id,mail,addAddressarr[0],addAddressarr[1],addAddressarr[2],addAddressarr[3],arr[3],arr[4],arr[0],arr[1]);
            out.println("修改成功");
        }
    }
}
